// 引入公共样式
require("../../assets/css/index.less");
// 引入本页样式
require("./findPwd.less");
// 引入验证码库
import CaptchaMini from "captcha-mini";
// 引入utils中的公共js
const dom = require("../../utils/dom.js");
const validator = require("../../utils/validator.js");
const https = require("../../utils/https.js");
// 判断是否有登录
dom.isToken();

dom.ready(function () {
	// 验证码
	let captcha1 = new CaptchaMini();
	let vcode = "";
	captcha1.draw(document.querySelector("#captcha1"), (r) => {
		console.log(r, "验证码1");
		vcode = r.toLowerCase();
	});
	// 切登录页面
	dom.goTo("#login", "login");
	// 表单验证部分
	// 用户名失焦验证
	let userFlag = false;
	dom.$("#username").onblur = function () {
		let account = dom.$("#username").value;
		if (validator.isNothing(account) && validator.isTel(account)) {
			this.parentNode.dataset.content = "";
			// 发ajax
			// https.post("/api/user/isExist", { account }, function (a) {
			// 	console.log(a);
			// });
			userFlag = true;
		} else {
			this.parentNode.dataset.content = "请输入正确的11位手机号";
		}
	};
	// 验证码失焦验证
	let vcodeFlag = false;
	dom.$("#vcode").onblur = function () {
		let vcodeVal = dom.$("#vcode").value;
		if (vcodeVal.toLowerCase() == vcode) {
			dom.$("#vcodeParent").dataset.content = "";
			vcodeFlag = true;
		} else {
			dom.$("#vcodeParent").dataset.content = "请输入正确的验证码";
		}
	};
	// 密码失焦验证
	let pwdFlag = false;
	dom.$("#pwd").onblur = function () {
		let pwdVal = dom.$("#pwd").value;
		if (validator.isNothing(pwdVal) && validator.isPwdOk(pwdVal)) {
			this.parentNode.dataset.content = "";
		} else {
			this.parentNode.dataset.content = "请输入正确的6-12位密码";
		}
	};
	dom.$("#pwdOk").onblur = function () {
		let pwdOkVal = dom.$("#pwdOk").value;
		let pwdVal = dom.$("#pwd").value;
		if (pwdOkVal == pwdVal && validator.isNothing(pwdOkVal)) {
			this.parentNode.dataset.content = "";
			pwdFlag = true;
		} else {
			this.parentNode.dataset.content = "密码不一致";
		}
	};
	// 全部都通过时才点击按钮才发ajax
	dom.$("#okBtn").addEventListener("click", function () {
		if (userFlag && vcodeFlag && pwdFlag) {
			alert("密码已修改,请登录");
			location.href = "login.html";
		} else {
			dom.$("#username").onblur();
			dom.$("#vcode").onblur();
			dom.$("#pwd").onblur();
			dom.$("#pwdOk").onblur();
		}
	});
});
